package com.iov.car.live;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;

public class LiveUtils {
    /**
     * 推流地址
     * 地址组成     推流域名  + AppName + StreamName+ 鉴权信息
     * 鉴权信息
     * RTMP 地址
     * rtmp://160242.livepush.myqcloud.com/AppName/StreamName?txSecret=md5(key+StreamName+hex(time))&txTime=hex(time)
     * WebRTC 地址
     * webrtc://160242.livepush.myqcloud.com/AppName/StreamName?txSecret=md5(key+StreamName+hex(time))&txTime=hex(time)
     * SRT 地址
     * srt://160242.livepush.myqcloud.com:9000?streamid=#!::h=160242.livepush.myqcloud.com,r=AppName/StreamName,txSecret=md5(key+StreamName+hex(time)),txTime=hex(time)
     * RTMP over SRT 地址
     * rtmp://160242.livepush.myqcloud.com:3570/AppName/StreamName?txSecret=md5(key+StreamName+hex(time))&txTime=hex(time)
     *
     * 拉流地址
     * webrtc://www.prositech.cn/AppName/StreamName
     *
     * http://www.prositech.cn/AppName/StreamName.m3u8
     * http://www.prositech.cn/2012A_Group1/game_111.m3u8
     */
    public static String PUSH_DOMAIN = "160242.livepush.myqcloud.com";//推流域名
    public static String PULL_DOMAIN = "www.prositech.cn";//拉流域名 需要备案

    public static String KEY = "b2eccccef6ec6166f694c59f092bc419";

    public static String getWebRTC_PushURL(String appName, String streamName, Date endTime){
        return "webrtc://" + PUSH_DOMAIN + "/" + appName+"/" + streamName+"?" + getSafeUrl(KEY,streamName,endTime.getTime()/1000);
    }

    public static String getWebRTC_PullURL(String appName,String streamName){
        return "webrtc://" + PULL_DOMAIN + "/" + appName+"/" + streamName;
    }

    public static String getM3U8_PullURL(String appName,String streamName){
        return "http://" + PULL_DOMAIN + "/" + appName+"/" + streamName + ".m3u8";
    }
    public static void main(String[] args) {
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DATE,1);
        Date tomorrow = calendar.getTime();
        System.out.println(now);
        System.out.println(tomorrow);

        String pushURL = getWebRTC_PushURL("yjs2103A_Group1","game_111", tomorrow);
        String pullURL = getM3U8_PullURL("yjs2103A_Group1","game_111");
        System.out.println(pushURL);
        System.out.println(pullURL);
    }

    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


    /*
     * KEY+ streamName + txTime
     */
    private static String getSafeUrl(String key, String streamName, long txTime) {
        String input = new StringBuilder().
                append(key).
                append(streamName).
                append(Long.toHexString(txTime).toUpperCase()).toString();


        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        return txSecret == null ? "" :
                new StringBuilder().
                        append("txSecret=").
                        append(txSecret).
                        append("&").
                        append("txTime=").
                        append(Long.toHexString(txTime).toUpperCase()).
                        toString();
    }


    private static String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }
}
